package com.es.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 使用statement接口执行sql
 */
public class TestStatement {
    //
    public static final String MYSQL_URL = "jdbc:mysql://127.0.0.1:3306/db2202";
    public static final String USER = "root";
    public static final String PASSWORD = "root";

    public static void main(String[] args) {
        //1.
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //2.获取连接
        // jdbc:mysql://ip:port/db
        try {
            //2.
            Connection connection = DriverManager.getConnection(MYSQL_URL, USER, PASSWORD);
            //3.
            Statement statement = connection.createStatement();
            //4.进行交互 ...
            //String ddl = "create table sys_user(id int primary key auto_increment, username varchar(32), password varchar(64))";
            //statement.execute(ddl);
            //测试其他
            String dml = "insert into sys_user(id,username,password) values (null, '张三', '123456')";
            String delete = "delete from sys_user where id = 11";
           // String sql = "select * from sys_user";
           // statement.execute(delete);
            //对于DML的操作尽量使用 executeUpdate
           // int i = statement.executeUpdate(delete);
            String update = "update sys_user set username='李四' where id = 2";
            int i = statement.executeUpdate(update);
            //System.out.println(i);
            if(i == 0){
                System.out.println("操作没有成功");
            }else{
                System.out.println("操作成功");
            }
            //5。释放资源
            statement.close();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
